import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-07-17
 * Time: 9:28
 */


public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root = null;

    public TreeNode createTree() {
        root = new TreeNode(10);
        TreeNode node1 = new TreeNode(6);
        TreeNode node2 = new TreeNode(14);
        TreeNode node3 = new TreeNode(4);
        TreeNode node4 = new TreeNode(8);
        TreeNode node5 = new TreeNode(12);
        TreeNode node6 = new TreeNode(16);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        node2.right = node6;

        return root;
    }

    public boolean search(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if(cur.val < val) {
                cur = cur.right;
            }else if(cur.val > val) {
                cur = cur.left;
            }else {
                return true;
            }
        }
        return false;
    }

    public boolean insert(int val) {
        TreeNode newNode = new TreeNode(val);
        if(root == null) root = newNode;
        TreeNode cur = root;
        TreeNode parent = root;
        while(cur != null) {
            if(val > cur.val) {
                parent = cur;
                cur = cur.right;
            }else if(val < cur.val) {
                parent = cur;
                cur = cur.left;
            }else {
                return false;
            }
        }
        if(val > parent.val) {
            parent.right = newNode;
        }else if(val < parent.val) {
            parent.left = newNode;
        }

        return true;
    }

    public boolean remove(int key) {
        TreeNode cur = root;
        TreeNode parent = root;
        while(cur != null) {
            if(key > cur.val) {
                parent = cur;
                cur = cur.right;
            }else if(key < cur.val) {
                parent = cur;
                cur = cur.left;
            }else {
                removeNode(parent,cur);
                return true;
            }
        }
        return false;
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = root.right;
            }else if(parent.left == cur) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = root.left;
            }else if(parent.left == cur) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode targetP = cur;
            TreeNode target = cur.right;
            while(target.left != null) {
                target = target.left;
            }
            cur.val = target.val;
            if(targetP.left == target) {
                targetP.left = target.right;
            }else {
                targetP.right = target.right;
            }
        }
    }

    public TreeNode Convert(TreeNode pRootOfTree) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode root = pRootOfTree;
        TreeNode head = null;
        TreeNode prev = null;
        boolean flg = false;
        while(root != null || !stack.isEmpty()) {
            while(root != null) {
                stack.push(root);
                root = root.left;
            }
            TreeNode top = stack.pop();
            if(!flg) {
                head = top;
                prev = top;
                flg = true;
            }else {
                prev.right = top;
                top.left = prev;
                prev = top;
            }
            root = top.right;
        }
        return head;
    }
}

